package com.foreveross.mofang.serviceconsole.core;

import java.util.Date;
import java.util.UUID;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

/**
 * 结点注册
 * 
 * @author fb
 */
@Entity
@Table(name = "MF_NODE_REGISTER")
public class NodeRegister extends AbstractEntity{
 
	/**
	 * 
	 */
	private static final long serialVersionUID = 8579976026089382782L;
	
	// 结点序列号
	@NotNull(message = "结点序列号不能为空!")
	@Size(max = 36, message = "结点序列号不能超过36个字符")
	@Column(name = "SN")
	private String nodeSn = UUID.randomUUID().toString();;
	
	// 结点名称
	@NotNull(message = "结点名称不能为空!")
	@Size(max = 150, message = "结点不能超过150个字符")
	@Column(name = "NAME")
	private String nodeName;
	
	// 结点ip
	@NotNull(message = "结点ip不能为空!")
	@Size(max = 15, message = "结点不能超过15个字符")
	@Column(name = "IP")
	private String ip;
	
	@NotNull(message = "结点端口不能为空!")
	@Column(name = "PORT")
	private Integer port;
	
	// 当前client适配器版本
	@NotNull(message = "结点适配器版本不能为空!")
	@Size(max = 150, message = "结点适配器版不能超过150个字符")
	@Column(name = "CLIENT_ADAPTERV_ERSION")
	private String clientAdapterVersion;
	
	// 当前server适配器版本
	@Column(name = "SERVER_ADAPTERV_ERSION")
	private String serverAdapterVersion;
	
	// 任务监控显示页面的url
	@NotNull(message = "任务监控url不能为空!")
	@Size(max = 150, message = "入口地址长度不能超过250个字符")
	@Pattern(regexp = "[http|https]+[://]+[0-9A-Za-z:/[-]_#[?][=][.][&]]*", message = "url格式错误")
	@Column(name = "TASK_URL", length = 500)
	private String taskUrl;
	
	// 注册时间
	@Column(name = "REGISTERDATE")
	private Date registerDate = new Date();
	
	// 最后一次跳连接时间
	@Column(name = "LAST_CONNDATE")
	private Date lastHeartbeatConnectionDate = new Date(); 
	
	public static void heartbeat(String nodeSn) {
		NodeRegister nodeRegister = getRepository().createJpqlQuery("select rg from NodeRegister rg where rg.nodeSn = :nodeSn order by rg.id desc").addParameter("nodeSn", nodeSn).<NodeRegister>list().get(0);
		nodeRegister.setLastHeartbeatConnectionDate(new Date());
		nodeRegister.save();
	}
	
	/**
	 * 判断最后一次心跳是否超过了指定的时长，如果离现在己超过时长则为离线
	 * 
	 * @param duration 超时进长 (分钟)
	 * @return
	 */
	public boolean isOline(int duration) {
		return duration > (new Date().getTime() - this.getLastHeartbeatConnectionDate().getTime())/60000;
	}
	
	public String getNodeSn() {
		return nodeSn;
	}

	public void setNodeSn(String nodeSn) {
		this.nodeSn = nodeSn;
	}

	public String getNodeName() {
		return nodeName;
	}

	public void setNodeName(String nodeName) {
		this.nodeName = nodeName;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public Integer getPort() {
		return port;
	}

	public void setPort(Integer port) {
		this.port = port;
	}

	public String getClientAdapterVersion() {
		return clientAdapterVersion;
	}

	public void setClientAdapterVersion(String clientAdapterVersion) {
		this.clientAdapterVersion = clientAdapterVersion;
	}

	public String getServerAdapterVersion() {
		return serverAdapterVersion;
	}

	public void setServerAdapterVersion(String serverAdapterVersion) {
		this.serverAdapterVersion = serverAdapterVersion;
	}

	public String getTaskUrl() {
		return taskUrl;
	}

	public void setTaskUrl(String taskUrl) {
		this.taskUrl = taskUrl;
	}

	public Date getRegisterDate() {
		return registerDate;
	}

	public void setRegisterDate(Date registerDate) {
		this.registerDate = registerDate;
	}

	public Date getLastHeartbeatConnectionDate() {
		return lastHeartbeatConnectionDate;
	}

	public void setLastHeartbeatConnectionDate(Date lastHeartbeatConnectionDate) {
		this.lastHeartbeatConnectionDate = lastHeartbeatConnectionDate;
	}
  
	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(this.nodeSn).append(getId())
				.toHashCode();
	}

	@Override
	public boolean equals(Object other) {
		System.out.println();
		if (!(other instanceof BaseEntity)) {
			return false;    
		}
		NodeRegister that = (NodeRegister) other;
		return new EqualsBuilder()
				.append(this.nodeSn, that.getNodeSn())
				.append(this.getId(), that.getId()).isEquals();
	}  
}
